<?php namespace App\Http\Controllers\Admin;

use App\Models\Catalog;
use Illuminate\Http\Request;
use App\Http\Requests\CatalogRequest;
use App\Http\Controllers\Controller;
use App\Services\CatalogService;

class CatalogController extends Controller
{

    public function getIndex(CatalogService $catalogService, Request $request)
    {
        return view('admin.catalog.catalogs', [
            'request' => $request,
            'catalogs' => $catalogService->getList($request),
        ]);
    }


    public function getAdd()
    {
        return view('admin.catalog.add', [
            'p_catalogs' => Catalog::where(['p_id' => 0, 'level' => 1])->get()
        ]);
    }

    public function postAdd(CatalogService $catalogService, CatalogRequest $catalogRequest)
    {
        $catalogService->create([
            'name' => $catalogRequest->input('name'),
            'p_id' => $catalogRequest->inaput('p_id', 0)
        ]);
        return $this->bSuccess('添加成功', true, ['tabid' => 'catalogs']);
    }

    public function getEdit($id)
    {
        return view('admin.catalog.edit', [
            'p_catalogs' => Catalog::where(['p_id' => 0, 'level' => 1])->get(),
            'catalog' => Catalog::find($id)
        ]);

    }

    public function postEdit(CatalogService $catalogService, CatalogRequest $catalogRequest)
    {
        $catalogService->update($catalogRequest->input('id'), [
            'name' => $catalogRequest->input('name'),
            'p_id' => $catalogRequest->input('p_id')
        ]);
        return $this->bSuccess('修改成功', true, ['tabid' => 'catalogs']);
    }

    public function getDelete($id, CatalogService $catalogService)
    {
        $catalogService->delete($id);
        return $this->bSuccess('删除成功', false);
    }
}